package wx.controller;

import admin.bean.Message;
import mvc.ResponseBody;
import util.JsonUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserController {

    @ResponseBody("/WxExpress/WxSendLoginCode.do")
    public String sendLoginCode(HttpServletRequest req, HttpServletResponse resp){
        //从微信用户登录页获取手机号
        String userPhone = req.getParameter("userPhone")==null?"":req.getParameter("userPhone");

        //发送验证码，这里假设发送了验证码123456
        String code = "123456";
        //验证码发送成功
        boolean flag = true;
        Message msg = new Message();

        if (flag){
            //验证码发送成功
            msg.setStatus(0);
            msg.setResult("验证码已发送，请查收~");
        }else {
            //验证码发送失败
            msg.setStatus(-1);
            msg.setResult("验证码下发失败，请检查手机号或稍后再试！");
        }

        String[] user = {userPhone,code};

        //存储登录session值用户的手机号userPhone,短信验证码code
        req.getSession().setAttribute("user",user);

        String json = JsonUtil.toJSON(msg);

        return json;
    }


    @ResponseBody("/WxExpress/Wxlogin.do")
    public String login(HttpServletRequest req, HttpServletResponse resp){
        //从微信用户登录页获取手机号和验证码
        String userPhone = req.getParameter("userPhone")==null?"":req.getParameter("userPhone");
        String code = req.getParameter("code")==null?"":req.getParameter("code");
        //获取登录时存储的session值
        String[] sysUser =(String[]) req.getSession().getAttribute("user");
        Message msg = new Message();
        if ("".equals(userPhone)){
            //输入的手机号为空
            msg.setStatus(-4);
            msg.setResult("输入的手机号码为空");
        }else if (sysUser[0].equals(userPhone)){
            //手机号验证正确后，进行其他验证
            if ("".equals(sysUser[1])){
                //这个手机号未获取短信
                msg.setStatus(-1);
                msg.setResult("手机号码未获取短信");
            }else if (sysUser[1].equals(code)){
                //手机号和验证码都验证一致时
                //判断用户的身份15180187276为管理员身份
                String[] userInfo = new String[2];
                if ("15180187276".equals(userPhone)){
                    //快递员
                    msg.setStatus(1);
                    msg.setResult("管理员登录");
                    userInfo[1] = "-1";
                }else {
                    //用户
                    msg.setStatus(0);
                    msg.setResult("普通用户登录");
                    userInfo[1] = "1";
                }

                userInfo[0] = userPhone;
                //存登录的手机号
                req.getSession().setAttribute("userInfo",userInfo);
            }else {
                //手机号正确，验证码验证不一致,登录失败
                msg.setStatus(-2);
                msg.setResult("输入的验证码有误，请检查！");
            }

        }else {
            //验证码一致，但输入的手机号与原获取的验证码手机号不一致时
            if (sysUser[1].equals(code)){
                msg.setStatus(-3);
                msg.setResult("输入的手机号有误，请检查！");
            }
        }

        String json = JsonUtil.toJSON(msg);

        return json;
    }

    @ResponseBody("/WxExpress/userInfo.do")
    public String userInfo(HttpServletRequest req, HttpServletResponse resp){

        String[] userInfo = (String[]) req.getSession().getAttribute("userInfo");
        String isuser = userInfo[1];
        Message msg = new Message();
        if (isuser.equals("1")){
            msg.setStatus(0);
        }else if(isuser.equals("-1")){
            msg.setStatus(1);
        }else {
            msg.setStatus(-1);
        }
        msg.setResult(userInfo[0]);
        String json = JsonUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/WxExpress/logout.do")
    public String logout(HttpServletRequest req, HttpServletResponse resp){
        //1.    销毁session
        req.getSession().invalidate();
        //2.    给客户端回复消息
        Message msg = new Message(0);

        return JsonUtil.toJSON(msg);
    }

}
